﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Web.Mvc;
using System.Web.Security;
using SmartParking.Data.Entities;
using ADEVSO.Util;
using SmartParking.Bussiness;
using System.ComponentModel;
using System.Web;

namespace SmartParking.Web.Models
{
    public class ParkZonesModel
    {
        public List<ParkZoneModel> ParkZones { get; set; }

        public ParkingModel Parking { get; set; }

        public ParkZonesModel()
        {

        }

        public ParkZonesModel(int parkingId = 0)
        {
            Parking parking = ParkingManager.GetParking(parkingId);

            Parking = new ParkingModel() { Address = (parking.Address != null ? parking.Address.Trim() : string.Empty), BankAccount = (parking.BankAccountNumber != null ? parking.BankAccountNumber.Trim() : string.Empty), CustomerID = parking.CustomerID, LegalId = (parking.LegalID != null ? parking.LegalID.Trim() : string.Empty), LegalName = (parking.LegalName != null ? parking.LegalName.Trim() : string.Empty), Name = (parking.Name != null ? parking.Name.Trim() : string.Empty), ParkingID = parkingId, PhoneNumber = (parking.Phone != null ? parking.Phone.Trim() : string.Empty) };
            ParkZones = ParkZoneModel.LoadParkZones(parkingId);
        }
    }

    public class ParkZoneModel
    {
        [Required]
        [Display(Name = "IdZona")]
        public int ParkZoneID { get; set; }

        [Required]
        [Display(Name = "IdParqueo")]
        public int ParkingID { get; set; }

        [Required(ErrorMessage = "Nombre de la zona de parqueo requerido.")]
        [Display(Name = "Nombre")]
        [DataType(DataType.Text)]
        [StringLength(30)]
        public string Name { get; set; }

        [Required(ErrorMessage = "Monto de la comisión requerido.")]
        [Display(Name = "Comisión del Administrador")]
        [DataType(DataType.Currency)]
        [StringLength(18)]
        public decimal ManagerCommisionAmount { get; set; }

        [Required(ErrorMessage = "Monto de la comisión web requerido.")]
        [Display(Name = "Comisión Web")]
        [DataType(DataType.Currency)]
        [StringLength(18)]
        public decimal WebCommisionAmount { get; set; }

        [Required]
        [Display(Name = "Permite fracción de hora?")]
        public bool AllowHalfHour { get; set; }

        [Required(ErrorMessage = "Hora de apertura requerida.")]
        [Display(Name = "Hora de apertura")]
        [DataType(DataType.Time)]
        [StringLength(6)]
        public string OpenHour { get; set; }

        [Required(ErrorMessage = "Hora de cierre requerida.")]
        [Display(Name = "Hora de cierre")]
        [DataType(DataType.Time)]
        [StringLength(6)]
        public string CloseHour { get; set; }

        [Required(ErrorMessage = "Cantidad de espacios regulares es requerida.")]
        [Display(Name = "Cantidad de espacios regulares")]
        public int RegularSpaces { get; set; }

        [Required(ErrorMessage = "Cantidad de espacios para reservar es requerida.")]
        [Display(Name = "Cantidad de espacios para reservar")]
        public int SpacesToReserve { get; set; }

        public ParkZoneModel()
        {

        }

        public static List<ParkZoneModel> LoadParkZones(int parkingId)
        {
            List<ParkZoneModel> Zones = new List<ParkZoneModel>();

            foreach (var zone in ParkingManager.GetParkingZones(parkingId))
            {
                Zones.Add(new ParkZoneModel() { AllowHalfHour = zone.AllowHalfHour, CloseHour = zone.CloseHour, ManagerCommisionAmount = zone.ManagerCommisionAmount, Name = zone.Name, OpenHour = zone.OpenHour, ParkingID = parkingId, ParkZoneID = zone.ParkingZoneID, RegularSpaces = zone.RegularSpaces, SpacesToReserve = zone.SpacesToReserve, WebCommisionAmount = zone.WebCommisionAmount });
            }

            return Zones;
        }
    }
}
